
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

using Swaf.BizObj;
using Swaf.Gui;
using Swaf.Security;

namespace Syrinx.Gui.AspNet
{
    [ToolboxData("<{0}:BizObjListDropDownDisplay runat=server></{0}:BizObjListDropDownDisplay>")]
	public class BizObjListDropDownDisplay : DropDownList, Swaf.Gui.IBizObjListDisplay
	{
		protected IBizObjList m_list;
		protected string m_resourceName;
		protected string m_fldName;
		protected IBizObj m_selectedBizObj;
        protected int m_maxElements = 0;

        public BizObjListDropDownDisplay()
		{
			this.SelectedIndexChanged += new EventHandler(BOListDropDownDisplay_SelectedIndexChanged);
		}
		void BOListDropDownDisplay_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.BizObjSelected(this, SelectedBizObj,SelectedBizObj!=null?SelectedBizObj.type.Name:"");
		}

		protected string m_parentIds;
		protected BizProcWidgetType m_procWidgetType = BizProcWidgetType.list;
		public string ParentBizProcIds { get { return m_parentIds; } set { m_parentIds = value; } }
		public BizProcWidgetType BizProcWidgetType { get { return m_procWidgetType; } set { m_procWidgetType = value; } }

		protected bool m_drivenByBizProc = true;
		public bool DrivenByBizProc { get { return m_drivenByBizProc; } set { m_drivenByBizProc = value; } }

		#region IBizObjListDropDownDisplay Members

		public string FieldNameDisplay
		{
			get { return m_fldName; }
			set { m_fldName = value; }
		}

		public void showBizObjs(IBusinessProcess bizproc, IBizObjCursor list)
		{
			m_list = list.Source;
			//m_fldName = displayField;

			string selectedId = Page.Request.Form[UniqueID];
            if (m_selectedBizObj != null)
                selectedId = m_selectedBizObj.KeyValue.ToString();

            this.Items.Clear();
            if (m_list != null)
                while(!list.eof)
				{
                    IBizObj obj = list.currentElement;
                    list.moveNext();

					ListItem itm = new ListItem();
					itm.Text =  obj.get(m_fldName).ToString();
					itm.Value = obj.KeyValue.ToString();
					if (itm.Value == selectedId)
						itm.Selected = true;
					this.Items.Add(itm);
				}

            //if (Page.Request.Form["__EVENTTARGET"] == UniqueID || !Page.IsPostBack)
            //    this.BizObjSelected(this, SelectedBizObj, "");
        }

		public void addBizObj(IBizObj bizObj)
		{
			if (bizObj != null)
			{
				ListItem itm = new ListItem();
				itm.Value = bizObj.KeyValue.ToString();
				itm.Text = bizObj.get(m_fldName).ToString();
				this.Items.Add(itm);
			}
		}

		public void removeBizObj(IBizObj bizObj)
		{
			if (bizObj != null)
				foreach (ListItem itm in this.Items)
					if (itm.Value.Equals(bizObj.get("id").ToString()))
						this.Items.Remove(itm);
		}

		public IBizObj SelectedBizObj
		{
			get
			{
				if (m_selectedBizObj == null && this.SelectedItem != null && m_list!=null)
					m_selectedBizObj = m_list.search(m_list.objectType.KeyFieldNames, m_list.objectType.convertValueToKeyType(this.SelectedItem.Value));
				return m_selectedBizObj;
			}
			set
			{
				setSelectedBizObj(value);
			}
		}

		public object SelectedBizObjKeyValue { get { return SelectedValue; } set { } }

		public void getSelectedBizObjs(IBizObjList selected)
		{
            //TODO: Implement this method
		}
		public void setSelectedBizObjs(IBizObjList selected)
		{
		}

		public string ResourceName
		{
			get
			{
				return m_resourceName;
			}
			set
			{
				m_resourceName = value;
			}
		}

		
		protected void setSelectedBizObj(IBizObj obj)
		{
			m_selectedBizObj = obj;
			for (int i = 0; i <= this.Items.Count - 1; i++)
				if (this.Items[i].Value == obj.KeyValue.ToString())
				{
					this.SelectedIndex = i;
                    this.SelectedItem.Text = obj.get(m_fldName).ToString();
				}
					
		}

		public virtual void updateBizObjs()
		{
			//Intentionally left blank. This class is read only.
		}

		public virtual void refresh()
		{
		}
		public string WidgetId { get { return UniqueID; } }

        public string MaxElements { get { return m_maxElements.ToString(); } set { m_maxElements = int.Parse(value); } }

        public event Swaf.Gui.BizObjSelectedInfo BizObjSelected;
		public event Swaf.Gui.BizObjChanged BizObjUpdated;

		public event Swaf.Gui.BizObjChanged BizObjDeleted;

		public event Swaf.Gui.BizObjChanged BizObjAdded;

        public Privileges EditPrivilege
        {
            get
            {
                return Privileges.ReadOnly;
            }
            set
            { }
        }

		#endregion


	}
}
